﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TechTree.Stalk.Roots.Database;
using TechTree.Stalk.Roots.Databases.Factories;
using TechTree.Stalk.Roots.Globalization.Interfaces;

namespace TechTree.Stalk.Roots.Globalization.Dictionaries
{
    public class DatabaseDictionary : ISourceDictionary
    {
        public string GetText(string key_)
        {
            return this.GetText(key_, CultureInfo.CurrentCulture);
        }

        public string GetText(string key_, CultureInfo cultureInfo_)
        {
            DatabaseContext context = DatabaseContextFactory.CreateDatabaseContext("teste");

            string retorno = String.Empty;

            using (IDbConnection dbConnection = context.CreateOpenConnection())
            {
                IDbCommand command = dbConnection.CreateCommand();
                command.CommandText = String.Format("select strValor from tbGlobalizacao where strNomeCultura='{0}' and strChave='{1}'", cultureInfo_.Name, key_);
                command.CommandType = CommandType.Text;

                IDataReader dataReader = command.ExecuteReader();

                if (dataReader != null)
                {
                    while (dataReader.Read())
                    {
                        return (string)dataReader.GetValue(0);
                    }
                }

                dbConnection.Close();
                dbConnection.Dispose();
            }

            return string.Empty;
        }
    }
}
